#ifndef chibi_h
#define chibi_h

#include <stdio.h>
#include <stdlib.h>
#include <lwan/lwan.h>
#include <lwan/lwan-mod-serve-files.h>
LWAN_HANDLER(chibi)
{
    response->mime_type = "text/html;charset=utf-8";
    const char *action = lwan_request_get_post_param(request, "action");
  
    const char *qinoc = lwan_request_get_post_param(request, "qino");
    if (strcmp(action,"addChibi") == 0)
    {
        const char *weijc = lwan_request_get_post_param(request, "weij");
        const char *weiyc = lwan_request_get_post_param(request, "weiy");
        const char *shujc = lwan_request_get_post_param(request, "shuj");
        const char *shuyc = lwan_request_get_post_param(request, "shuy");
        const char *wujc = lwan_request_get_post_param(request, "wuj");
        const char *wuyc = lwan_request_get_post_param(request, "wuy");

        if (qinoc == NULL || weijc == NULL || weiyc == NULL || shujc == NULL || shuyc == NULL || wujc == NULL || wuyc == NULL)
        {
            static const char noarg[] = "2";
            lwan_strbuf_set_static(response->buffer, noarg, sizeof(noarg) - 1);
            return HTTP_OK;
        }
        unsigned int qino = atoi(qinoc);
        unsigned int weij = atoi(weijc);
        unsigned long long int weiy = atoll(weiyc);
        unsigned int shuj = atoi(shujc);
        unsigned long long int shuy = atoll(shuyc);
        unsigned int wuj = atoi(wujc);
        unsigned long long int wuy = atoll(wuyc);
        if (model_chibi_add(qino, weij, weiy, shuj, shuy, wuj, wuy) == 0)
        {

            static const char success[] = "0";
            lwan_strbuf_set_static(response->buffer, success, sizeof(success) - 1);
        }
        else
        {
            static const char fail[] = "1";
            lwan_strbuf_set_static(response->buffer, fail, sizeof(fail) - 1);
        }
    }
    else if (strcmp(action,"updateVictory")==0)
    {
         const char *victoryc = lwan_request_get_post_param(request, "victory");
          if (victoryc == NULL)
        {
            static const char noarg[] = "2";
            lwan_strbuf_set_static(response->buffer, noarg, sizeof(noarg) - 1);
            return HTTP_OK;
        }
         unsigned int victory = atoi(victoryc);
        unsigned int qino = atoi(qinoc);
      
        if (model_chibi_update_victory(qino, victory) == 0)
        {
            static const char success[] = "0";
            lwan_strbuf_set_static(response->buffer, success, sizeof(success) - 1);
        }
        else
        {
            static const char fail[] = "1";
            lwan_strbuf_set_static(response->buffer, fail, sizeof(fail) - 1);
        }

    }

    return HTTP_OK;
}

#endif